package laba7.taskB;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Demonstration of Executors.
 *
 * @author yakimovich
 */
public class Main {

    public static final int NUMBER_OF_TASKS = 15;
    public static final int NUMBER_OF_EXECUTORS = 3;

    /**
     * Demonstration of Executors.
     *
     * @param args
     */
    public static void main(String[] args) {
        TasksStorageImpl storage = new TasksStorageImpl();
        Random random = new Random();
        for (int i = 0; i < NUMBER_OF_TASKS; i++) {
            if (random.nextInt() % 3 == 0) {
                storage.add(new CopyTaskImpl(
                        // the fine sourse, tasks is exequted:
                         new File("source.txt"), //comment this

                        // the bad sourse, tasks is failed:
//                        new File("sourceBad.txt"), // uncomment this
                        new File("dest" + i + ".txt")));
            } else {
                storage.add(new SumTaskImpl());
            }
        }
        System.out.println("The number of tasks in the storage: "
                + storage.count());
        System.out.println();

        List<ExecutorImpl> executors = new ArrayList<>();
        for (int i = 0; i < NUMBER_OF_EXECUTORS; i++) {
            ExecutorImpl ex = new ExecutorImpl();
            ex.setStorage(storage);
            executors.add(ex);
            ex.start();
        }

        while (storage.count() > 0) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException ex) {
                System.out.println(ex);
            }
        }

        for (ExecutorImpl ex : executors) {
            ex.stop();
        }

        System.out.println("\nThe number of tasks in the storage: "
                + storage.count());
    }
}
